package com.javastudy.forum.service.impl;

import com.javastudy.forum.common.AppResult;
import com.javastudy.forum.common.ResultCode;
import com.javastudy.forum.dao.BoardMapper;
import com.javastudy.forum.exception.ApplicationException;
import com.javastudy.forum.model.Board;
import com.javastudy.forum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.Date;
import java.util.List;

/**
 * @author Chooker
 * @create 2023-08-17 19:40
 */
@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        if (num < 0 || num == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> boards = boardMapper.selectByNum(num);

        return boards;
    }

    @Override
    public Board selectById(Long id) {
        if (id < 0 || id == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if (id < 0 || id == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = selectById(id);
        if (board == null) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_EXISTS));
        }
        board.setArticleCount(board.getArticleCount() + 1);
        board.setUpdateTime(new Date());
        int row = boardMapper.updateByPrimaryKeySelective(board);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if (id < 0 || id == null) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = selectById(id);
        if (board == null) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_EXISTS));
        }
        board.setArticleCount(board.getArticleCount() - 1);
        if (board.getArticleCount() < 0) {
            board.setArticleCount(0);
        }
        board.setUpdateTime(new Date());
        int row = boardMapper.updateByPrimaryKeySelective(board);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }
}
